import 'package:enterprise_v6_app/app/components/custom_app_bar.dart';
import 'package:enterprise_v6_app/app/components/custom_modele/custom_search_enter.dart';
import 'package:enterprise_v6_app/app/components/custom_refresh.dart';
import 'package:enterprise_v6_app/app/components/dialog/filter_dialog/custom_filter_right_sheet.dart';
import 'package:enterprise_v6_app/app/components/special_operation/custom_title_chart_page.dart';
import 'package:enterprise_v6_app/app/core/ext/ext.dart';
import 'package:enterprise_v6_app/app/core/values/dimens.dart';
import 'package:enterprise_v6_app/app/core/values/gaps.dart';
import 'package:enterprise_v6_app/app/core/values/images.dart';
import 'package:enterprise_v6_app/app/data/entities/application/basic_information/component_management_ledger_entity.dart';
import 'package:enterprise_v6_app/app/modules/application/basic_information/common/basic_common_widget.dart';
import 'package:enterprise_v6_app/app/modules/application/basic_information/equipment_facilities/component_management_ledger/component_management_ledger_controller.dart';
import 'package:enterprise_v6_app/router/app_pages.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:flutter_svg/svg.dart';
import 'package:get/get.dart';

///部件管理台账
class ComponentManagementLedgerPage extends StatelessWidget {
  ComponentManagementLedgerPage({super.key});

  final controller = Get.put(ComponentManagementLedgerController());

  ///获取条件搜索项
  Widget _getSearchConditionWidget() {
    return Row(
      children: [
        Expanded(
            child: CustomSearchEnter(
          height: 32.w,
          hintText: '请输入名称',
          text: controller.searchX,
          onCallBack: (value) {
            controller.searchX = value;
            controller.refreshPage();
          },
        )),
        AppGaps.hGap8,
        _screen(),
      ],
    );
  }

  ///筛选
  Widget _screen() {
    return Container(
      width: 36.w,
      height: 32.w,
      alignment: Alignment.center,
      decoration: BoxDecoration(color: Colors.white, borderRadius: BorderRadius.circular(8.w)),
      child: SvgPicture.asset(
        AppImages.icon_search_screen,
        width: 18.w,
        height: 16.w,
      ),
    ).clickOnce(controller.screen);
  }

  /// 获取字段显示
  Widget _getTextView({required String title, required String value, Color? valueColor}) {
    return Row(
      children: [
        Text(
          title,
          style: TextStyle(
            fontSize: 13.sp,
            height: 1.2,
            fontWeight: FontWeight.w400,
            color: const Color(0xffA5A6B5),
          ),
        ),
        Expanded(
            child: Container(
          alignment: Alignment.centerRight,
          child: Text(
            value,
            style: TextStyle(
              fontSize: 12.sp,
              height: 1.2,
              fontWeight: FontWeight.w400,
              color: valueColor ?? const Color(0xff495061),
            ),
          ),
        ))
      ],
    );
  }

  @override
  Widget build(BuildContext context) {
    return GetBuilder(
        init: controller,
        builder: (_) {
          return Scaffold(
            appBar: CustomAppBar(
              titleText: '部件管理台账',
            ),
            key: controller.popRightScaffoldKey,
            endDrawer: Container(
              width: AppDimens.sw - 70.w,
              height: AppDimens.sh,
              color: Colors.white,
              child: _getFilterWidget(),
            ),
            body: Container(
              color: const Color(0xffF5F5F6),
              padding: EdgeInsets.symmetric(horizontal: 16.w, vertical: 12.w),
              child: Column(
                children: [
                  _getSearchConditionWidget(),
                  AppGaps.vGap12,
                  Expanded(
                      child: CustomEasyRefreshList<ComponentManagementLedgerEntity>(
                    key: controller.globalKey,
                    dataLoad: controller.componentManagementLedgerList,
                    itemBuilder: (ComponentManagementLedgerEntity item, int index) {
                      return CustomTitleChartPage(
                        title: item.componentName,
                        titleRightWidget: BasicCommonWidget.getCommonStateView(item.auditStatus),
                        child: Column(
                          crossAxisAlignment: CrossAxisAlignment.start,
                          children: [
                            _getTextView(title: '检定：', value: _type(item.type)),
                            AppGaps.vGap12,
                            _getTextView(title: '操作人：', value: item.inspector),
                          ],
                        ).clickOnce(() {
                          Get.toNamed(AppRoutes.componentManagementLedgerDetailPage, arguments: {'id': item.id})?.then((value) {
                            if (value != null) {
                              controller.refreshPage();
                            }
                          });
                        }),
                      );
                    },
                  ))
                ],
              ),
            ),
          );
        });
  }

  ///获取右侧筛选样式
  Widget _getFilterWidget() {
    return CustomFilterRightSheet(
      dataList: controller.filterList,
      onConfirm: controller.screenConfirm,
      isRadio: true,
    );
  }

  /// 类型：1.检修 2.检验 3.检定 4.更换
  String _type(int type) {
    switch (type) {
      case 1:
        return '检修';
      case 2:
        return '检验';
      case 3:
        return '检定';
      case 4:
        return '更换';
    }
    return '';
  }
}
